package com.work.libtest2.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.work.libtest2.dto.DepartmentDTO;
import com.work.libtest2.entity.Department;
import com.work.libtest2.entity.R;
import com.work.libtest2.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: DeptController
 * Package: com.work.libtest2.controller
 * Description:
 *
 * @Author
 * @Create 2024/6/10 8:45
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/department")
public class DepartmentController {
    @Resource
    private DepartmentService deptService;


    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize, Department department){
        IPage<DepartmentDTO> page = deptService.getPage(currentPage,pageSize,department);
        if (currentPage>page.getPages()){
            page = deptService.getPage((int)page.getPages(),pageSize,department);
        }
        return new R(true,page);
    }
    @GetMapping("/getAllDeptName")
    public R getAllDeptName(){
        List<Department> deptList =  deptService.getAll();
        return new R(true,deptList);
    }

   //删除
   @DeleteMapping("{deptId}")
    public R deleteDeptById(@PathVariable  Integer deptId){
        int i = deptService.DeleteDepById(deptId);
        if (i>0){
            return new R(true);
        }else return new R(false);
    }

    //新增
    @PostMapping
     public R Save(@RequestBody Department department){
        int i = deptService.SaveDepartment(department);
        return new R(i>0,i>0?"保存成功":"保存失败");
    }

    //查找
    @GetMapping("{deptId}")
    public R FindById(@PathVariable  Integer deptId){
        DepartmentDTO department= deptService.findDepByID(deptId);
        return new R(true,department);
    }

    //更新
    @PutMapping
     public R UpdateDept(@RequestBody  Department department){
       int i = deptService.updateDep(department);
       return new R(i>0);
    }

}
